package com.object.oriented.exam.system5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.object.oriented.exam.system1.dao.ProblemMapping;
import com.object.oriented.exam.system1.entity.Problem;
import com.object.oriented.exam.system2.dao.ProblemPropertyMapping;
import com.object.oriented.exam.system2.entity.ProblemProperty;
import com.object.oriented.exam.system2.service.ProblemPropertyService;
import com.object.oriented.exam.system5.entity.Answer;
import com.object.oriented.exam.system5.entity.AnswerVo;
import com.object.oriented.exam.system5.entity.Score;
import com.object.oriented.exam.system5.mapper.AnswerMapper;
import com.object.oriented.exam.system5.mapper.ScoreMapper;
import com.object.oriented.exam.system5.service.AnswerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xdf
 * @date 2023-06-14
 */
@Service
public class AnswerServiceImpl implements AnswerService {
    @Resource
    AnswerMapper answerMapper;

    @Resource
    ScoreMapper scoreMapper;

    @Resource
    ProblemMapping problemMapping;

    @Resource
    ProblemPropertyMapping  problemPropertyMapping;

    @Override
    public Boolean markAnswer(Answer answer) {
        int result=0;
        if(answerMapper.selectById(answer.getId())!=null){
           result = answerMapper.updateById(answer);
        }
        return result!=0;
    }

    @Override
    public Boolean commitAnswer(Answer answer) {
        QueryWrapper<Answer> answerQueryWrapper=new QueryWrapper<>();
        answerQueryWrapper
                .eq("exam_id",answer.getExamId())
                .eq("question_id",answer.getQuestionId())
                .eq("student_id",answer.getStudentId())
                ;
        Answer pre=answerMapper.selectOne(answerQueryWrapper);
        if(pre!=null){
            pre.setAnswer(answer.getAnswer());
            return answerMapper.updateById(pre)>0;
        }
        answer.setResult((byte) 0);
        return answerMapper.insert(answer)>0;
    }

    @Override
    public Page<AnswerVo> getAnswer(Long studentId, Long examId, Long current, Long size) {
        QueryWrapper<Answer> answerQueryWrapper=new QueryWrapper<>();
        answerQueryWrapper
                .eq(!ObjectUtils.isEmpty(studentId),"student_id",studentId)
                .eq(!ObjectUtils.isEmpty(examId),"exam_id",examId)
                .orderByDesc("id")
                ;
        Page<Answer> answerPage=new Page<>(current,size);
        answerMapper.selectPage(answerPage,answerQueryWrapper);
        List<Answer> answers=answerPage.getRecords();
        List<AnswerVo> answerVos=new LinkedList<>();
        for (Answer answer:answers){
            QueryWrapper<ProblemProperty> propertyQueryWrapper=new QueryWrapper<>();
            propertyQueryWrapper
                    .eq("question_id",answer.getQuestionId());
            Problem problem=problemMapping.selectById(answer.getQuestionId());
            ProblemProperty problemProperty=problemPropertyMapping.selectOne(propertyQueryWrapper);
            AnswerVo answerVo=new AnswerVo();
            BeanUtils.copyProperties(answer,answerVo);
            answerVo.setTitle(problem.getTitle());
            answerVo.setExplanation(problemProperty.getExplanation());
            answerVos.add(answerVo);
        }
        Page<AnswerVo> resultPage=new Page<>(current,size);
        resultPage.setPages(answerPage.getPages());
        resultPage.setTotal(answerPage.getTotal());
        resultPage.setRecords(answerVos);
        return resultPage;
    }

    @Override
    public List<Long> getStudent(Long examId, Long current, Long size) {
        return getAnswer(null,examId,current,size).getRecords().stream().map(Answer::getStudentId).distinct().collect(Collectors.toList());
    }

    @Override
    public Page<AnswerVo> getAnswer(Long studentId, Long current, Long size) {
        return getAnswer(studentId,null,current,size);
    }

    @Override
    public Page<Answer> getErrorAnswer(Long studentId, Long current, Long size) {
        QueryWrapper<Answer> answerQueryWrapper=new QueryWrapper<>();
        answerQueryWrapper
                .eq("student_id",studentId)
                .eq("result",1)
                .orderByDesc("id")
                ;
        Page<Answer> resultPage=new Page<>(current,size);
        answerMapper.selectPage(resultPage,answerQueryWrapper);
        return resultPage;
    }

    @Override
    public Integer totalAnswer(Long studentId) {
        QueryWrapper<Answer> answerQueryWrapper=new QueryWrapper<>();
        answerQueryWrapper
                .eq("student_id",studentId);
        return answerMapper.selectCount(answerQueryWrapper);
    }

}
